const downloadBase64Image = (base64Data: string, fileName: string) => {
  // 创建一个临时的a标签
  const a = document.createElement("a");

  // 将Base64数据转化为Blob对象
  const byteCharacters = atob(base64Data.split(",")[1]); // 去掉前缀部分（data:image/png;base64,）
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += 1024) {
    const slice = byteCharacters.slice(offset, offset + 1024);
    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }
    byteArrays.push(new Uint8Array(byteNumbers));
  }

  const blob = new Blob(byteArrays, { type: "image/png" }); // 创建Blob对象
  const url = URL.createObjectURL(blob); // 创建URL对象

  a.href = url;
  a.download = fileName; // 设置文件名
  a.click(); // 模拟点击下载

  // 下载完成后清除临时URL
  URL.revokeObjectURL(url);
};

export default downloadBase64Image;
